﻿using System;

using HL.Lib.MVC;
using HL.Lib.Models;

namespace HL.Lib.Controllers
{
    public class MSearchController : Controller
    {
        [HL.Core.MVC.PropertyInfo("Số lượng")]
        public int PageSize = 10;

        public void ActionIndex(MSearchModel model)
        {
            var dbQuery = ModNewsService.Instance.CreateQuery()
                    .Where(o => o.Activity == true)
                    .Where(model.Area>0,o=>o.AreaID==model.Area)
                    .Where(!string.IsNullOrEmpty(model.Keyword),o=>o.Name.Contains(model.Keyword)||o.Summary.Contains(model.Keyword))
                    .Where(model.Capacity > 0, o => o.Capacity == model.Capacity)
                    //.Where(model.Promotion == 1, o => o.Promotion == true)
                    .WhereIn(model.Services>0,o => o.MenuID, WebMenuService.Instance.GetChildIDForWeb_Cache("News", model.Services, ViewPage.CurrentLang.ID))
                    .OrderByDesc(o => o.Order)
                    .Take(PageSize)
                    .Skip(PageSize * model.Page);

            ViewBag.Data = dbQuery.ToList();
            model.TotalRecord = dbQuery.TotalRecord;
            model.PageSize = PageSize;
            ViewBag.Model = model;
            ViewPage.ViewBag.M = model;
        }
    }

    public class MSearchModel
    {
        private int _Page = 0;
        public int Page
        {
            get { return _Page; }
            set { _Page = value - 1; }
        }

        public int TotalRecord { get; set; }
        public int PageSize { get; set; }

        public int Services { get; set; }
        public int Area { get; set; }
        public int Capacity { get; set; }
        public int Promotion { get; set; }
        public string Keyword { get; set; }
    }
}
